﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace lipesin.Main
{
	public partial class Transaction : Form
	{
		public enum Type
		{
			Transfer,
			Expence
		}

		private long? _transactionId;
		private Type _type;
		private DataTable _transaction;
		private DataTable _transactionDetail;
		private DataTable _categories;

		public Transaction()
		{
			InitializeComponent();
		}

		public void Init(long? transactionId, Type type)
		{
			_transactionId = transactionId;
			using (SqlCommand cmd = new SqlCommand())
			{
				cmd.CommandText =
					@"	select * from dbo.category";
						//where (GetDate() between  date_from and date_to or date_to is null)";
				_categories = Dalc.ExecuteToDataTable(cmd);

				if (!_transactionId.HasValue)
				{
					_type = type;
					cmd.CommandText = "select * from transaction where 2=3";
					_transaction = Dalc.ExecuteToDataTable(cmd);
					_transaction.Rows.Add(_transaction.NewRow());
					cmd.CommandText = "select * from transaction_detail where 2=3";
					_transactionDetail = Dalc.ExecuteToDataTable(cmd);
					if (_type == Type.Transfer) _transactionDetail.Rows.Add(_transactionDetail.NewRow());
				}
				else
				{
					cmd.CommandText = "select * from transaction where id = {0}".F(_transactionId);
					_transaction = Dalc.ExecuteToDataTable(cmd);
					cmd.CommandText = "select * from transaction_detail where transaction_id = {0}".F(_transactionId);
					_transactionDetail = Dalc.ExecuteToDataTable(cmd);
					_type = Type.Transfer;
					if (_transactionDetail.Rows.Count != 1)
						_type = Type.Expence;
					else
					{
						DataRow[] sresult = _categories.Select("id = {0}".F(_transactionDetail.Rows[0]["category_id"]));
						if (Convert.ToInt64(sresult[0]["type"]) != 1) _type = Type.Expence;
					}
				}

				if (_type == Type.Transfer)
				{
					grdExpences.Visible = false;
					lblRecepient.Text = "Получатель";
					this.Height = cmbRecepient.Bottom + 10;
				}
				else
				{
					grdExpences.Top = cmbRecepient.Top;
					cmbRecepient.Visible = false;
					lblRecepient.Text = "Получатели";
					this.Height = grdExpences.Bottom + 10;
				}
			}
		}
	}
}
